package com.roads.site.controller;

import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.utils.SecurityUtils;
import com.roads.project.domain.ProInstitution;
import com.roads.project.domain.ProReport;
import com.roads.project.domain.bid.BidSelect;
import com.roads.project.service.IBidService;
import com.roads.project.service.IProInstitutionService;
import com.roads.project.service.IProReportService;
import com.roads.site.domain.SiteLabourBattalion;
import com.roads.site.domain.SiteLabourPost;
import com.roads.site.domain.SiteLabourTeam;
import com.roads.site.service.ISiteLabourBattalionService;
import com.roads.site.service.ISiteLabourPostService;
import com.roads.site.service.ISiteLabourTeamService;
import com.roads.system.domain.DictNation;
import com.roads.system.domain.DictProvince;
import com.roads.system.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName SiteLabourReportController
 * @Description 报表controller
 * @Author nhp
 * @Date 2022/11/17 0010 11:14
 * @Version 1.0.0
 **/
@RestController
@RequestMapping("/labour/report")
public class SiteLabourReportController extends BaseController {

    @Autowired
    private IBidService bidService;

    @Autowired
    private IProReportService reportService;

    @Autowired
    private IProInstitutionService institutionService;

    @Autowired
    private ISiteLabourBattalionService battalionService;

    @Autowired
    private ISiteLabourTeamService teamService;

    @Autowired
    private ISiteLabourPostService postService;

    @Autowired
    private IDictService dictService;

    /**
     * 初始化报表页面
     *
     * @param reportType 报表类型
     * @return 报表集合
     */
    @GetMapping("/init")
    public AjaxResult init(String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        boolean isAdmin = SecurityUtils.isAdmin(getUserId());
        // 初始化标段
        List<BidSelect> bids = bidService.selectProjectBidBranch(getUsername(), isAdmin);
        ajaxResult.put("bids", bids);
        if (bids.size() > 0) {
            String institutionCode = bids.get(0).getBids().get(0).getCode();
            ajaxResult.put("institutionCode", institutionCode);
            // 初始化报表列表
            long userId = 0L;
            ProInstitution institution = institutionService.getModelByInnerCode(institutionCode);
            if (!SecurityUtils.isAdmin(getUserId())) {
                userId = getUserId();
            }
            List<ProReport> list = reportService.selectReportList(institution.getProjectId(), userId, reportType);
            ajaxResult.put("list", list);
        }
        return ajaxResult;
    }

    /**
     * 获取报表列表
     *
     * @param reportType 报表类型
     * @param institutionCode 机构编号
     * @return 报表集合
     */
    @GetMapping("/getReportList")
    public AjaxResult getReportList(String institutionCode, String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        ProInstitution institution = institutionService.getModelByInnerCode(institutionCode);
        long userId = 0L;
        if (!SecurityUtils.isAdmin(getUserId())) {
            userId = getUserId();
        }
        List<ProReport> list = reportService.selectReportList(institution.getProjectId(), userId, reportType);
        ajaxResult.put("list", list);
        return ajaxResult;
    }

    /**
     * 初始化报表视图页面
     *
     * @param institutionCode 机构编号
     * @param reportType 报表类型
     * @return 机构、工期、用户集合
     */
    @GetMapping("/initStaffView")
    public AjaxResult initStaffView(String institutionCode, String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        boolean isAdmin = SecurityUtils.isAdmin(getUserId());
        // 初始化标段
        List<BidSelect> bids = bidService.selectProjectBidBranch(getUsername(), isAdmin);
        ajaxResult.put("bids", bids);
        if (bids.size() > 0) {
            ajaxResult.put("institutionCode", institutionCode);
            // 加载施工队伍、施工班组、岗位工种
            List<SiteLabourBattalion> battalions = battalionService.dropDownList(institutionCode);
            List<SiteLabourTeam> teams = teamService.dropDownList(institutionCode);
            List<SiteLabourPost> posts = postService.dropDownList(institutionCode);
            ajaxResult.put("battalions", battalions);
            ajaxResult.put("teams", teams);
            ajaxResult.put("posts", posts);
        }
        List<DictNation> nations = dictService.getNationList();
        ajaxResult.put("nations", nations);
        List<DictProvince> provinces = dictService.getProvinceList();
        ajaxResult.put("provinces", provinces);
        return ajaxResult;
    }

    /**
     * 初始化报表视图页面
     *
     * @param institutionCode 机构编号
     * @param reportType 报表类型
     * @return 机构、工期、用户集合
     */
    @GetMapping("/initSalaryView")
    public AjaxResult initSalaryView(String institutionCode, String reportType) {
        AjaxResult ajaxResult = new AjaxResult();
        boolean isAdmin = SecurityUtils.isAdmin(getUserId());
        // 初始化标段
        List<BidSelect> bids = bidService.selectProjectBidBranch(getUsername(), isAdmin);
        ajaxResult.put("bids", bids);
        if (bids.size() > 0) {
            ajaxResult.put("institutionCode", institutionCode);
            // 加载施工队伍、施工班组、岗位工种
            List<SiteLabourBattalion> battalions = battalionService.dropDownList(institutionCode);
            List<SiteLabourTeam> teams = teamService.dropDownList(institutionCode);
            List<SiteLabourPost> posts = postService.dropDownList(institutionCode);
            ajaxResult.put("battalions", battalions);
            ajaxResult.put("teams", teams);
            ajaxResult.put("posts", posts);
        }
        return ajaxResult;
    }

}
